from django.http import JsonResponse
import json

def dispatcherBase(request, action_handler_dict):
    if 'usertype' not in request.session:
        return JsonResponse({
            'ret': 302,
            'msg': '未登录',
            'redirect': '/mgr/sign.html'
        }, status=302)
        # return HttpResponseRedirect('/mgr/sign.html')

    if request.session['usertype'] != 'mgr':
        return JsonResponse({
            'ret': 302,
            'msg': '用户非管理员类型',
            'redirect': '/mgr/sign.html'
        }, status=302)


    if request.method == 'GET':
        request.params = request.GET
    elif request.method in ['POST', 'PUT', 'DELETE']:
        request.params = json.loads(request.body)
    
    action = request.params['action']
    if action in action_handler_dict:
        return action_handler_dict[action](request)
    else:
        return JsonResponse({'ret': 1, 'msg': 'action参数错误'})